#include <fstream>
#include <iostream>
#include <vector>

namespace part1
{

std::vector<std::int64_t> getNumbers(const std::string& line)
{
    std::vector<std::int64_t> numbers {};
    std::size_t index {0};
    while (index < line.size())
    {
        const char& ch {line[index]};
        if (ch == ' ')
        {
            index += 1;
            continue;
        }

        // Find next not digit or the end of line
        std::size_t pos {index + 1};
        while (true)
        {
            if (pos >= line.size() || line[pos] == ' ')
                break;
            pos += 1;
        }
        // Get number
        numbers.emplace_back( std::stoi(&ch) );
        index = pos;
    }
    return numbers;
}

class RaceData
{
public:
    RaceData(std::int64_t t, std::int64_t d) : 
        _time{t}, _distance{d} {}

    std::size_t getBeatNumber() const 
    {
        std::size_t number {0};
        for (std::int64_t holdTime = 0; holdTime <= this->_time; ++holdTime)
        {
            std::int64_t travelTime {this->_time - holdTime};
            std::int64_t speed {holdTime};
            if (travelTime * speed >= this->_distance)
                number += 1;
        }
        return number;
    }

private:
    std::int64_t _time;
    std::int64_t _distance;

public:
    std::int64_t time() const noexcept
    { return this->_time; }
    
    std::int64_t distance() const noexcept
    { return this->_distance; }
};

std::vector<RaceData> getData(std::ifstream& file)
{
    std::string line;

    std::getline(file, line);
    std::size_t timeFirstColon {line.find(":")};
    std::vector<std::int64_t> times {getNumbers( line.substr(timeFirstColon + 1, line.size() - timeFirstColon - 1))};

    std::getline(file, line);
    std::size_t disFirstColon {line.find(":")};
    std::vector<std::int64_t> distances {getNumbers( line.substr(disFirstColon + 1, line.size() - disFirstColon - 1))};

    std::vector<RaceData> datas {};
    for (std::size_t i = 0; i < times.size(); ++i)
        datas.emplace_back(times[i], distances[i]);
    
    return datas;
}

void solute()
{
    std::ifstream file {"./src/day06/input.txt"};
    std::vector<RaceData> datas {getData(file)};

    std::size_t result {1};
    for (const RaceData& data : datas)
    {
        std::cout << "time: " << data.time() << ", distance: " << data.distance() << std::endl;
        result *= data.getBeatNumber();
    }

    std::cout << result << std::endl;
}

}